//
//  ChannelCellFrameViewModel.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/10.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ChannelCellFrameViewModel.h"
#import "ChannelContent.h"
#import "ChannelOfChannel.h"
#import "ChannelImg.h"
#import "ChannelTags.h"
#import "TAMacro.h"
#import "SelfSizeTool.h"

@interface ChannelCellFrameViewModel ()

@property (nonatomic, assign) CGFloat currentHeight;

@end


@implementation ChannelCellFrameViewModel

- (void)setChannelContent:(ChannelContent *)channelContent {
    _channelContent = channelContent;
    
    // 计算
    [self setUpHeaderImageViewFrame];
    [self setUpVipImageViewFrame];
    [self setUpNameLabelFrame];
    [self setUpContentImageViewFrame];
    [self setUpImgNumLabelFrame];
    [self setUpDescLabelFrame];
    [self setUpTagsLabelFrame];
    [self setUpChannelContentViewFrame];
    [self setUpToolViewFrame];
    [self setUpHotCommentViewFrame];
    [self setUpCellHeight];
    
}

// 计算头像
- (void)setUpHeaderImageViewFrame {
    CGFloat imageX = ViewMargin;
    CGFloat imageY = ViewMargin;
    CGFloat imageW = 30;
    CGFloat imageH = 30;
    _headerImageViewFrame = CGRectMake(imageX, imageY, imageW, imageH);
    
}
// 计算会员标志
- (void)setUpVipImageViewFrame {
    CGFloat imageX = CGRectGetMaxX(_headerImageViewFrame) - 6;
    CGFloat imageY = CGRectGetMaxY(_headerImageViewFrame) - 10;
    CGFloat imageW = 12;
    CGFloat imageH = 12;
    _vipImageViewFrame = CGRectMake(imageX, imageY, imageW, imageH);
    
}
// 计算用户昵称
- (void)setUpNameLabelFrame {
    CGFloat nameX  = CGRectGetMaxX(_headerImageViewFrame) + ViewMargin;
    CGFloat nameY = ViewMargin;
    CGFloat nameW = 200;
    CGFloat nameH = 30;
    _nameLabelFrame = CGRectMake(nameX, nameY, nameW, nameH);
}
// 计算图片
- (void)setUpContentImageViewFrame {
    
    ChannelImg *img = _channelContent.img.firstObject;
    CGFloat scale = img.w / (SCREEN_WIDTH - 20);
    CGFloat height = img.h / scale;

    CGFloat imageX = 0;
    CGFloat imageY = CGRectGetMaxY(_headerImageViewFrame) + ViewMargin;
    CGFloat imageW = SCREEN_WIDTH - 20;
    CGFloat imageH = ceil(height);
    _contentImageViewFrame = CGRectMake(imageX, imageY, imageW, imageH);
    self.currentHeight = CGRectGetMaxY(_contentImageViewFrame);
}
// 图片个数
- (void)setUpImgNumLabelFrame {
    
    CGFloat labelX = CGRectGetMaxX(_contentImageViewFrame) - 45;
    CGFloat labelY = CGRectGetMaxY(_contentImageViewFrame) - 30;
    CGFloat labelW = 40;
    CGFloat labelH = 25;
    _imgNumLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}
// 计算文本
- (void)setUpDescLabelFrame {
    CGFloat labelX = 10;
    CGFloat labelY = _currentHeight;
    CGFloat labelW = SCREEN_WIDTH - 40;
    CGFloat labelH = 0;
    if ([_channelContent.desc isEqualToString:@""]) {
    } else {
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:_channelContent.desc width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:14]];
        labelH = height + 2 * ViewMargin;
        self.currentHeight += labelH;
    }
    _descLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
}
// 计算标签
- (void)setUpTagsLabelFrame {
    
    if (_channelContent.tag.count != 0) {
        CGFloat labelX = 10;
        CGFloat labelY = _currentHeight;
        CGFloat labelW = SCREEN_WIDTH - 40;
        CGFloat labelH = 0;
        NSMutableArray *tagNameArray = [NSMutableArray array];
        for (NSInteger i = 0; i < _channelContent.tag.count; i++) {
            [tagNameArray addObject:_channelContent.tag[i].content];
        }
        [tagNameArray insertObject:@"#" atIndex:0];
        NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:tagString width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:13]];
        labelH = height + 2 * ViewMargin;
        self.currentHeight += labelH;
        _tagsLabelFrame = CGRectMake(labelX, labelY, labelW, labelH);
    }
}

- (void)setUpChannelContentViewFrame {
    CGFloat viewX = 10;
    CGFloat viewY = 0;
    CGFloat viewW = SCREEN_WIDTH - 20;
    CGFloat viewH = self.currentHeight  + ViewMargin;
    _channelContentViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    
}

// 计算工具栏
- (void)setUpToolViewFrame {
    CGFloat viewX = 10;
    CGFloat viewY = _currentHeight;
    CGFloat viewW = SCREEN_WIDTH - 20;
    CGFloat viewH = 50;
    _toolViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
    self.currentHeight += viewH;
}

- (void)setUpHotCommentViewFrame {
    if (_channelContent.hotComments.count != 0) {
        _hotCommentCellHeight = 20;
        CGFloat viewX = 10;
        CGFloat viewY = _currentHeight;
        CGFloat viewW = SCREEN_WIDTH - 20;
        CGFloat viewH = _channelContent.hotComments.count * 20 + ViewMargin;
        _hotCommentViewFrame = CGRectMake(viewX, viewY, viewW, viewH);
        _currentHeight += viewH;
    }
}

// 计算cell高
- (void)setUpCellHeight {
    _cellHeight = _currentHeight + ViewMargin;
}


@end
